//
//  ByViewClassPartScroll.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/11/1.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewSearchPartScroll.h"

@implementation ByCellClassPartItem
{
    UILabel *_contentLabel;
    UIView  *_line;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.adjustsFontSizeToFitWidth = true;
    _contentLabel.numberOfLines = 1;
    _contentLabel.font = FONT(14);
    _contentLabel.textAlignment = NSTextAlignmentCenter;
    _contentLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    _line = [[UIView alloc] init];
    _line.backgroundColor = BY_LINE_COLOR;
    [self.contentView addSubview:_line];
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
        make.bottom.equalTo(@0);
        make.height.equalTo(@1);
    }];
 
    
}

- (void)setContent:(NSString *)content {
    
    _content = content;
    
    _contentLabel.text = _content;
}
- (void)setSelecting:(BOOL)selecting {
    
    _selecting = selecting;

    self.backgroundColor = _selecting ? BY_BG_COLOR : [UIColor whiteColor];
    _line.backgroundColor = _selecting ? BY_GOLD_COLOR : BY_LINE_COLOR;
    
    [_line mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(_selecting ? 2 : 1);
    }];
}



@end


@implementation ByViewSearchPartScroll
{
    NSInteger _selectingIndex;
}
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    
    if (self = [super initWithFrame:frame style:style]) {
        
        [self setupDefault];
    }
    return self;
}
// 创建表视图
- (void)setupDefault {
    
    _selectingIndex = 0;
    
    self.backgroundColor = [UIColor whiteColor];
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.delegate = self;
    self.dataSource = self;
    self.tableFooterView = [UIView new];
}



#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ByCellClassPartItem *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[ByCellClassPartItem alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.selecting = (indexPath.row == _selectingIndex);
    
    cell.content = self.titles[indexPath.row];
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    // 点击当前的
    if (_selectingIndex == indexPath.row) {
        return;
    }
    
    _selectingIndex = indexPath.row;
    
    [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
    [self reloadData];
    
    if (self.didSelectPart) {
        self.didSelectPart(self.titles[indexPath.row]);
    }
}

@end
